<?php
/**
 * Implementation of custom_create_content() 
 * @TODO to be discarded
 * 
 * */
function custom_create_content() {
    global $user;
    $bundles = node_get_types('names');
    $list = variable_get('c_user_bundles', $bundles);

    return theme('page__create_content', $list);   
}

/**
 * Implementation of custom_change_templates() 
 * A list of all available themes
 *
 * @TODO Premium templates have to be implemented
 * 
 * */
function custom_change_templates() {  
    $list = array();
    $sql = db_query("SELECT * FROM `system` WHERE type = 'theme'");
    while($result = db_fetch_object($sql)) {
        $list[] = $result;
    }
    return theme('page__change_templates', $list); 
}

/**
 * Implementation of custom_set_template()
 * 
 *
 **/

function custom_set_template() {
    $name = filter_xss($_GET['name'], array());
   
    db_query("DELETE FROM {fb_user_theme} WHERE page_id = '%s' LIMIT 1", $_SESSION['page_data']['page']['id']);
    db_query("INSERT INTO {fb_user_theme} VALUES ('%s', '%s')", $_SESSION['page_data']['page']['id'], $name);
    
    $_SESSION['user_theme'] = $name;
    session_write_close();
    drupal_goto('change-templates');
    exit();
}

/**
 * Implementation of order_list()
 * Retrieves all the user's sold items
 * 
 **/
function order_list() {
    global $user;
    
    $header = array(
      array('data' => t('Actions')),
      array('data' => t('Order ID'), 'field' => 'o.order_id', 'sort' => 'desc'),
      array('data' => t('Customer')),
      array('data' => t('Total'), 'align' => 'center', 'field' => 'o.order_total'),
      array('data' => t('Purchase date'), 'align' => 'center', 'field' => 'o.created'),
      array('data' => t('Status'), 'field' => 'os.title'),
    );
  
    if (is_null($sql)) {
      $args = array();
      $show_status = 1;
      $sql = 'SELECT o.order_id, o.uid, o.billing_first_name, o.billing_last_name, o.order_total, o.order_status, o.created, os.title '
            .'FROM {uc_orders} o INNER JOIN {uc_order_products} uop ON uop.order_id = o.order_id INNER JOIN {node} n ON n.nid = uop.nid LEFT JOIN {uc_order_statuses} os ON o.order_status = os.order_status_id ';
  
  
      if (arg(3) == 'sort' && !is_null(arg(4))) {
        $_SESSION['sort_status'] = arg(4);
        $args[] = arg(4);
      }
      $where = '';
      if (!isset($_SESSION['sort_status']) || $_SESSION['sort_status'] != 'all') {
        if (!empty($_SESSION['sort_status']) && is_string($_SESSION['sort_status'])) {
          $where = "WHERE o.order_status = '%s'";
          $args[] = $_SESSION['sort_status'];
        }
        else {
          $where = 'WHERE o.order_status IN '. uc_order_status_list('general', TRUE);
        }
      }
      $add = ' AND n.uid = '.$user->uid;
      $sql .= $where .$add. tablesort_sql($header);
    }
    
    $address = variable_get('uc_customer_list_address', 'billing');
    if ($address == 'shipping') {
        $sql = str_replace('billing', 'delivery', $sql);
    }
    else {
      $address = 'billing';
    }
  
    $context = array(
      'revision' => 'themed-original',
      'type' => 'amount',
    );
  
    $rows = array();
  
    $result = pager_query($sql, variable_get('uc_order_number_displayed', 30), 0, NULL, $args);
    while ($order = db_fetch_object($result)) {
      if ($address == 'shipping') {
        $order_name = $order->delivery_first_name .' '. $order->delivery_last_name;
      }
      else {
        $order_name = $order->billing_first_name .' '. $order->billing_last_name;
      }
      if (trim($order_name) == '') {
        if ($order->uid !== 0) {
          $account = db_result(db_query("SELECT name FROM {users} WHERE uid = %d", $order->uid));
        }
        if (empty($account)) {
          $order_name = t('User: none');
        }
        else {
          $order_name = t('User: !name', array('!name' => $account));
        }
      }
  
      $rows[] = array(
        'data' => array(
          array('data' => uc_order_actions($order, TRUE)),
          array('data' => $order->order_id),
          array('data' => check_plain($order_name)),
          array('data' => uc_price($order->order_total, $context), 'align' => 'right'),
          array('data' => format_date($order->created, 'custom', variable_get('uc_date_format_default', 'm/d/Y')), 'align' => 'center'),
          array('data' => $order->title),
        ),
        'id' => 'order-'. $order->order_id,
      );
    }
  
    drupal_add_js(array(
      'ucURL' => array(
        'adminOrders' => url('admin/store/orders/'),
      ),
    ), 'setting');
    drupal_add_js(drupal_get_path('module', 'uc_order') .'/uc_order.js');
  
    $output = '';
    if ($search === FALSE) {
      $output = '<div class="order-overview-form">'. drupal_get_form('uc_order_select_form') .'</div>'
               .'<div class="order-overview-form">'. drupal_get_form('uc_order_admin_sort_form') .'</div>';
    }
    
    $output .= theme('table', $header, $rows, array('class' => 'uc-orders-table'));
    $output .= theme('pager', NULL, variable_get('uc_order_number_displayed', 30), 0);
    return $output;
}